package com.easemob.cases;

import android.content.Context;

import com.easemob.cases.db.MessageDBHelper;
import com.easemob.cases.db.MessageUserTable;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.model.EaseAtMessageHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageModel {
    MessageUserTable mUser = null;
    protected Context context = null;
    protected Map<Key, Object> valueCache = new HashMap<Key, Object>();

    public MessageModel(Context ctx) {
        context = ctx;
        MessagePreferenceManager.init(context);
    }

    public boolean saveContactList(List<EaseUser> contactList) {
        MessageDBHelper.getInstance().saveContactList(contactList);
        return true;
    }

    public Map<String, EaseUser> getContactList() {
        return MessageDBHelper.getInstance().getContactList();
    }

    public void saveContact(EaseUser user) {
        MessageDBHelper.getInstance().saveContact(user);
    }

    /**
     * save current username
     *
     * @param username
     */
    public void setCurrentUserName(String username) {
        MessagePreferenceManager.getInstance().setCurrentUserName(username);
    }

    public String getCurrentUsernName() {
        return MessagePreferenceManager.getInstance().getCurrentUserName();
    }

    public void setDisabledGroups(List<String> groups) {
        List<String> list = new ArrayList<String>();
        list.addAll(groups);
        //直接删除groups的内容，可能会有并发的错误
        synchronized (list) {
            for (int i = 0; i < list.size(); i++) {
                if (EaseAtMessageHelper.get().getAtMeGroups().contains(list.get(i))) {
                    list.remove(i);
                    i--;
                }
            }
        }

        MessageDBHelper.getInstance().setDisabledGroups(groups);
        valueCache.put(Key.DisabledGroups, list);
    }

    public List<String> getDisabledGroups() {
        Object val = valueCache.get(Key.DisabledGroups);

        if (val == null) {
            val = MessageDBHelper.getInstance().getDisabledGroups();
            valueCache.put(Key.DisabledGroups, val);
        }

        return (List<String>) val;
    }

    public void setDisabledIds(List<String> ids) {
        MessageDBHelper.getInstance().setDisabledIds(ids);
        valueCache.put(Key.DisabledIds, ids);
    }

    public List<String> getDisabledIds() {
        Object val = valueCache.get(Key.DisabledIds);
        if (val == null) {
            val = MessageDBHelper.getInstance().getDisabledIds();
            valueCache.put(Key.DisabledIds, val);
        }

        return (List<String>) val;
    }

    public void setGroupsSynced(boolean synced) {
        MessagePreferenceManager.getInstance().setGroupsSynced(synced);
    }

    public boolean isGroupsSynced() {
        return MessagePreferenceManager.getInstance().isGroupsSynced();
    }

    public void setContactSynced(boolean synced) {
        MessagePreferenceManager.getInstance().setContactSynced(synced);
    }

    public boolean isContactSynced() {
        return MessagePreferenceManager.getInstance().isContactSynced();
    }

    public void setBlacklistSynced(boolean synced) {
        MessagePreferenceManager.getInstance().setBlacklistSynced(synced);
    }

    public boolean isBacklistSynced() {
        return MessagePreferenceManager.getInstance().isBacklistSynced();
    }

    enum Key {
        DisabledGroups,
        DisabledIds
    }
}
